﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Shsc.Net.Common.Exeption;

namespace Shsc.Net.Common.Log
{
    public class Log4NetLogger
    {
        private static ILog _logger = null;

        private static IDictionary<Type, Log4NetLogger> LoggerMap
            = new Dictionary<Type, Log4NetLogger>();

        private Log4NetLogger(Type type)
        {
            _logger = LogManager.GetLogger(type.Name);
        }

        public static Log4NetLogger CreateLogger(Type type)
        {
            if (!LoggerMap.ContainsKey(type))
            {
                LoggerMap.Add(type, new Log4NetLogger(type));
            }
            return LoggerMap[type];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void Debug(String message)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug(message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void Warn(String message)
        {
            if (_logger.IsWarnEnabled)
            {
                _logger.Warn(message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void Info(String message)
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.Info(message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public void Error(String message, Exception ex)
        {
            if (_logger.IsErrorEnabled)
            {
                _logger.Error(message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public void Fatal(Exception ex)
        {
            if (_logger.IsFatalEnabled)
            {
                string message = string.Empty;
                if (ex is BaseException)
                {
                    message = ((BaseException)ex).Message;
                }
                _logger.Fatal(message, ex);
            }
        }

        public bool IsDebugEnabled
        {
            get { return _logger.IsDebugEnabled; }
        }

        public bool IsErrorEnabled
        {
            get { return _logger.IsErrorEnabled; }
        }

        public bool IsFatalEnabled
        {
            get { return _logger.IsFatalEnabled; }
        }

        public bool IsInfoEnabled
        {
            get { return _logger.IsInfoEnabled; }
        }

        public bool IsWarnEnabled
        {
            get { return _logger.IsWarnEnabled; }
        }
    }
}
